package cn.community.system.web.controller;

import cn.community.system.domain.*;
import cn.community.system.query.MemberQuery;
import cn.community.system.service.IAssociationService;
import cn.community.system.service.IDepartmentService;
import cn.community.system.service.IMemberService;
import cn.community.system.service.IMemberVoService;
import cn.community.system.util.JsonResult;
import cn.community.system.util.UserContext;
import cn.community.system.vo.AssoMemberVo;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/member")
public class MemberController {
    @Autowired
    private IMemberService memberService;

    @Autowired
    private IMemberVoService memberVoService;

    @Autowired
    private IAssociationService associationService;

    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping("/list")
    @RequiresPermissions(value = {"member/list","超管-社团成员信息列表"},logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") MemberQuery qo){
        model.addAttribute("page",memberService.query(qo));
        return "user/member/list";
    }

    @RequestMapping("/listAsso")
    @RequiresPermissions(value = {"member/listAsso","社团-社团成员信息列表"},logical = Logical.OR)
    public String listAsso(Model model, @ModelAttribute("qo") MemberQuery qo){
        model.addAttribute("page",memberService.queryByAssoId(qo));
        return "user/member/listAsso";
    }

    @RequestMapping("/input")
    @RequiresPermissions(value = {"member/input","超管-社团成员个人信息页面"},logical = Logical.OR)
    public String input(Model model,Long id){
        if(id!= null){ //编辑
            model.addAttribute("member",memberService.get(id));
            model.addAttribute("all",memberVoService.selectByMemberId(id));
        }
        return "user/member/input";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"member/saveOrUpdate","超管-社团成员个人信息的编辑功能"},logical = Logical.OR)
    public Object saveOrUpdate(Member member){
        if(member.getId() == null){ // 代表新增
            memberService.save(member);
        }else { // 代表修改
            memberService.myUpdate(member);
        }
        return JsonResult.success();
    }

    @RequestMapping("/saveOrUpdateRelation")
    @ResponseBody
    @RequiresPermissions(value = {"member/saveOrUpdateRelation","超管-社团成员信息新增/编辑功能"},logical = Logical.OR)
    public Object saveOrUpdateRelation(AssoMemberVo assoMemberVo){
        MemberVo memberVo = new MemberVo();
        memberVo.setIsAgree(assoMemberVo.getIsAgree());
        memberVo.setPassTime(assoMemberVo.getPassTime());
        memberVo.setMemberId(assoMemberVo.getId());
        memberVo.setName(assoMemberVo.getName());
        Long assoId = assoMemberVo.getAssoId();
        Association association = associationService.get(assoId);
        memberVo.setAssoId(assoId);
        memberVo.setAssoname(association.getUsername());
        String deptname = assoMemberVo.getDeptname();
        memberVo.setDeptname(deptname);
        Department department = departmentService.getByAssoIdAndDeptname(assoId, deptname);
        if(department != null){
            memberVo.setDeptId(department.getId());
        }

        if(assoMemberVo.getRelationId() == null){ // 代表新增
            memberVoService.save(memberVo);
        }else { // 代表修改
            memberVo.setId(assoMemberVo.getRelationId());
            memberVoService.update(memberVo);
        }

        return JsonResult.success();
    }

    @RequestMapping("/profile")
    @RequiresPermissions(value = {"member/profile","社团成员-个人中心页面"},logical = Logical.OR)
    public String profile(Model model){
        Member member = UserContext.getCurrentMember();
        model.addAttribute("member",memberService.get(member.getId()));
        model.addAttribute("all",memberVoService.selectByMemberId(member.getId()));
        return "profile/profileMember";
    }

    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(AssoMemberVo assoMemberVo){
        if(assoMemberVo.getId() != null){
            memberService.edit(assoMemberVo);
        }
        return JsonResult.success();
    }

    @RequestMapping("/update")  // 个人中心修改
    @ResponseBody
    @RequiresPermissions(value = {"member/update","社团成员-个人中心编辑功能"},logical = Logical.OR)
    public Object update(Member member){
        if(member.getId() != null){ // 代表修改
            memberService.myUpdate(member);
        }
        return JsonResult.success();
    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    @RequiresPermissions(value = {"member/batchDelete","社团成员信息批量删除"},logical = Logical.OR)
    public JsonResult batchDelete(Long[] ids){
        memberVoService.batchDelete(ids);
        return JsonResult.success();
    }

    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"member/delete","社团成员信息删除"},logical = Logical.OR)
    public JsonResult delete(Long id){
        memberVoService.deleteByPrimaryKey(id);
        return JsonResult.success();
    }

    @RequestMapping("/importXls")
    @ResponseBody
    @RequiresPermissions(value = {"member/importXls","社团成员信息Excel导入"},logical = Logical.OR)
    public JsonResult importXls(MultipartFile file) throws Exception {
        memberService.importXls(file);
        return JsonResult.success();
    }

    @RequestMapping("/exportXls")
    @RequiresPermissions(value = {"member/exportXls","超管-社团成员信息Excel导出"},logical = Logical.OR)
    public void exportXls(HttpServletResponse response) throws Exception {
        //文件下载的响应头
        response.setHeader("Content-Disposition","attachment;filename=member.xls");
        //直接使用poi
        //创建excel文件
        Workbook wb = memberService.exportXls();
        //把excel的数据输出给浏览器
        wb.write(response.getOutputStream());
    }

    @RequestMapping("/exportXls2")
    @RequiresPermissions(value = {"member/exportXls2","社团-社团成员信息Excel导出"},logical = Logical.OR)
    public void exportXls2(HttpServletResponse response) throws Exception {
        //文件下载的响应头
        response.setHeader("Content-Disposition","attachment;filename=member.xls");
        //直接使用poi
        //创建excel文件
        Workbook wb = memberService.exportXls2();
        //把excel的数据输出给浏览器
        wb.write(response.getOutputStream());
    }

    // 审核
    @RequestMapping("/passOne")
    @ResponseBody
    @RequiresPermissions(value = {"member/passOne","社团成员信息审核"},logical = Logical.OR)
    public JsonResult passOne(Integer hot,Long id,Long assoId){
        memberService.passOne(id,assoId,hot);
        return JsonResult.success();
    }

    // 批量审核
    @RequestMapping("/bitchPass")
    @ResponseBody
    @RequiresPermissions(value = {"member/bitchPass","社团成员信息批量审核"},logical = Logical.OR)
    public JsonResult bitchPass(Long[] ids){
        memberVoService.bitchPass(ids);
        return JsonResult.success();
    }

    @RequestMapping("/imgUrl")
    @ResponseBody
    public JsonResult imgUrl(){
        Member member = UserContext.getCurrentMember();
        String imgUrl = memberService.get(member.getId()).getImgUrl();
        return JsonResult.success(imgUrl);
    }

}
